<?php

namespace App\Http\Models\Report;

use App\Http\Models\Business\CachePayerScale;
use App\Http\Models\Business\Payer;

class PayerReportPayerScale extends Payer
{

    //基础数据
    public $payer_scale_array = [];

    public $total_scale_amount = 0;

    //结果数据， 键名为 品类id, 键值为 规模金额
    public $payer_scale = [];

    public function addPayerScale(CachePayerScale $model)
    {
        $this->payer_scale_array[] = $model;
    }


    public function setPayerScaleResultForSearch()
    {
        foreach ($this->payer_scale_array as $payer_scale) {
            $this->total_scale_amount += $payer_scale->amount;
            if (array_key_exists($payer_scale->category_contract_id, $this->payer_scale)) {
                $this->payer_scale[$payer_scale->category_contract_id] += $payer_scale->amount;
            } else {
                $this->payer_scale[$payer_scale->category_contract_id] = $payer_scale->amount;
            }
        }
    }

    public function getPayerScaleResult(int $category_contract_id)
    {
        $result = 0;
        if (isset($this->payer_scale[$category_contract_id])) {
            $result = $this->payer_scale[$category_contract_id];
        }
        return $result;
    }

    public function getPayerScaleProportion(int $category_contract_id)
    {
        $result = 0;
        $current_category_amount = 0;
        if (isset($this->payer_scale[$category_contract_id])) {
            $current_category_amount = $this->payer_scale[$category_contract_id];
        }
        if ($this->total_scale_amount != 0) {
            $result = $current_category_amount / $this->total_scale_amount;
        }
        return round_percent($result);
    }
}
